package com.guanyong.webapi.controllers.api;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.qiniu.cdn.CdnManager;
import com.qiniu.util.StringMap;
import org.apache.commons.codec.binary.Hex;
/**
 * Created by Administrator on 2017/9/18 0018.
 */
public class qiniuDemo {

        /**
         * 生成资源基于CDN时间戳防盗链的访问外链
         *
         * @param
         * @param
         * @throws MalformedURLException
         * @throws UnsupportedEncodingException
         * @throws NoSuchAlgorithmException
         */

        public static String getAntiLeechAccessUrlBasedOnTimestamp(String url, String encryptKey, int durationInSeconds)
                throws MalformedURLException, UnsupportedEncodingException, NoSuchAlgorithmException {
            URL urlObj = new URL(url);
            String path = urlObj.getPath();

            long timestampNow = System.currentTimeMillis() / 1000 + durationInSeconds;
//            long timestampNow = 0x55bb9b80;
            String expireHex = Long.toHexString(timestampNow);

            String toSignStr = String.format("%s%s%s", encryptKey, path, expireHex);
            System.out.println("toSignStr:"+toSignStr);
            String signedStr = md5ToLower(toSignStr);
            System.out.println("signedStr:"+signedStr);

            String signedUrl = null;
            if (urlObj.getQuery() != null) {
                signedUrl = String.format("%s&sign=%s&t=%s", url, signedStr, expireHex);
            } else {
                signedUrl = String.format("%s?sign=%s&t=%s", url, signedStr, expireHex);
            }
            return signedUrl;
        }

        private static String md5ToLower(String src) throws UnsupportedEncodingException, NoSuchAlgorithmException {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(src.getBytes("utf-8"));
            byte[] md5Bytes = digest.digest();
            return Hex.encodeHexString(md5Bytes);
        }




    public static void main(String[] args) {
        String encryptKey = "c08b68b89c9824996b9a90b80f9b1c9883bab188";
//        String encryptKey = "4db9d1b92da96d9813990dab8e994b8b68a8998b";
//        String encryptKey = "12345678";
        // 待加密链接
//        String fileKey = "修道院惊魂 片花.mp4";
        String fileKey = "ToSaveaLife.mp4";
//        String fileKey = "notviode.mp4";
//        String fileKey = "X500_clip.mp4";
//        String fileKey = "DIR1/中文/vodfile.mp4";
//        String fileKey = "test.txt";
        String encodedFileKey;
        try {
            // 考虑到文件名称会有中文，所以需要做urlencode
            encodedFileKey = URLEncoder.encode(fileKey, "utf-8");
            encodedFileKey=encodedFileKey.replaceAll("\\+", "%20");
            encodedFileKey=encodedFileKey.replaceAll("%2F", "/");
//            encodedFileKey=encodedFileKey+"?v=1.2";
//            String urlToSign = String.format("http://test.fdybee.com/%s", encodedFileKey);
            String urlToSign = String.format("http://video.fdybee.com/%s", encodedFileKey);
//            String urlToSign = String.format("http://xxx.yyy.com/%s", encodedFileKey);
            // 有效期
            int duration = 3600;

            String signedUrl = qiniuDemo.getAntiLeechAccessUrlBasedOnTimestamp(urlToSign, encryptKey, duration);
            System.out.println(signedUrl);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }






}
